<?php	
	require "row.php";
	// global variable - result of the output functions are dumped here. Use output() to print it.
	$_html = "";
	
	/*
		Clears the output
	*/
	
	function outputClear() {
		global $_html;
		$_html = "";
	}
	
	/*
		Appends the arguments to the global $_html output variable.
	*/
	function outputRaw() {
		global $_html;
		$args = func_get_args();
		$html = "";
		foreach ($args as $arg) {
			$html .= $arg;
		}
		$_html .= $html;
		return $html;
	}
	
	/*
		Outputs a text wrapped in a tag. Takes two or three arguments. First always specifies the tag, in which the text
		will be embedded. The last is always the text.
		
		If there are three arguments, the second is a dictionary containing the pairs attribute-value.
	*/
	function outputTag($tag, $a, $b = "") {
		global $_html;
		
		$html = "<$tag";
		$n = func_num_args();
		if ($n == 2) {
			$html .= ">$a</$tag>";
		} elseif ($n == 3) {
			// 3 arguments
			foreach($a as $attr => $value) $html .= " $attr=\"$value\"";
			
			$html .= ">$b</$tag>";
		}
		
		$_html .= $html;
		return $html;
	}
	

	/* 
		Takes an SQL result and transforms the data in it into a table. Make sure the result has the same number
		of columns that are entered, there is no check for this. Difference will cause the html table to break.
		The row formatting function is the name of the function, which generates the HTML for each row of the result.
		The default row formatter is "rowPlain", see "inc/row.php".
	
		
		Returns generated HTML. 
		
			res			SQL result, as returned from mysql_query();
			columns		Array of column names
			row_func	The row formatting function. 
			<params>	Any paramaters to be passed to the row formatting function.
			
	*/
	
	function outputSQLTable(&$res, $columns, $row_func = "rowPlain") {
		global $_html;
		// begin table
		$html = "<table border=\"1\">";
		
		// column names
		$html .= "<tr>";
		foreach($columns as $col) {
			$html .= "<th>$col</th>";			
		}
		$html .= "</tr>";
		
		// extract parameters for the row formatting function
		if (func_num_args() > 3) {
			for ($i = 3; $i < func_num_args(); $i++) {
				$params[$i-3] = func_get_arg($i);
			}
		} else {
			$params = "";
		}
		
		// row formatter
		while ($row = mysql_fetch_row($res)) {
			// pass control to the row formatting function
			$html .= $row_func($row, $params);
		}
		
		// end
		$html .= "</table>";
		
		// return
		$_html .= $html;
		return $html;
	}
	
	/*
		Outputs a whole file.
	*/
	function outputFile($file) {
		global $_html;
		$fp = fopen($file,"r");
		$txt = fread($fp, filesize($file));
		
		$_html .= $txt;
		return $txt;
	}
	
	/* 
		Outputs the global $_html variable.
	*/
	function output() {
		global $_html;
		echo $_html;
	}
?>